package com.system.cacheClient.client.impl;

import com.system.cacheClient.client.SetCacheClient;
import com.system.cacheClient.common.CacheR;
import com.system.cacheClient.feign.SetCacheFeignClient;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * Set缓存客户端实现类
 */
@Component
@RequiredArgsConstructor
public class SetCacheClientImpl implements SetCacheClient {
    
    private final SetCacheFeignClient setCacheFeignClient;
    
    @Override
    public <T> CacheR<Long> sadd(String key, T... values) {
        return setCacheFeignClient.sadd(key, values);
    }
    
    @Override
    public <T> CacheR<Long> srem(String key, T... values) {
        return setCacheFeignClient.srem(key, values);
    }
    
    @Override
    public <T> CacheR<T> spop(String key) {
        return setCacheFeignClient.spop(key);
    }
    
    @Override
    public CacheR<Set<String>> smembers(String key) {
        return setCacheFeignClient.get(key);
    }
    
    @Override
    public <T> CacheR<Boolean> sismember(String key, T member) {
        CacheR<Set<String>> result = setCacheFeignClient.get(key);
        if (result.getCode() == 200 && result.getData() != null) {
            boolean isMember = result.getData().contains(member.toString());
            return CacheR.success(isMember, "检查成员是否存在成功");
        }
        return CacheR.fail(false, "检查成员是否存在失败");
    }
    
    @Override
    public CacheR<Long> scard(String key) {
        CacheR<Set<String>> result = setCacheFeignClient.get(key);
        if (result.getCode() == 200 && result.getData() != null) {
            long size = result.getData().size();
            return CacheR.success(size, "获取集合大小成功");
        }
        return CacheR.fail(0L, "获取集合大小失败");
    }
}